package com.breaktian.assemble.utils;

import android.util.Log;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by breaktian on 2016/10/26.
 *
 * 使用LogUtils打印log  默认tag为类名  传入tag参数的未改造
 * 可打印出类名，时间 方法 行号
 *  03-24 10:54:22.669 19654-19654/com.zhongan.insurance D/RepayListFragment.java: zalog [10:54:22]:[eventCallback:237]RepayListFragment eventID:3028,eventTag:RepayListFragment,resultCode:-1,resultMsg:网络异常
 */

public class LogUtil {

    public static boolean isDebug = true;

    private static String className;
    private static String methodName;
    private static int lineNumber;

    public static String DEFAULT_SIMPLE_TIME_FORMAT2 = "HH:mm:ss";

    /**
     * 获取时分秒
     * */
    public static String getDateTime(long time) {
        if (time <= 0) {
            return "";
        }
        Date date = new Date();
        date.setTime(time);
        SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_SIMPLE_TIME_FORMAT2);
        return sdf.format(date);
    }


    private static String createLog(String log ) {

        StringBuffer buffer = new StringBuffer("Log ["+getDateTime(System.currentTimeMillis())+"]:");
        buffer.append("[");
        buffer.append(methodName);
        buffer.append(":");
        buffer.append(lineNumber);
        buffer.append("]");
        buffer.append(log);
        return buffer.toString();
    }

    private static void getMethodNames(StackTraceElement[] sElements){
        className = sElements[1].getFileName();
        methodName = sElements[1].getMethodName();
        lineNumber = sElements[1].getLineNumber();
    }


    public static void e(String message){
        if(isDebug){
            // Throwable instance must be created before any methods
            getMethodNames(new Throwable().getStackTrace());
            Log.e(className, createLog(message));
        }
    }

    public static void i(String message){
        if(isDebug){
            // Throwable instance must be created before any methods
            getMethodNames(new Throwable().getStackTrace());
            Log.i(className, createLog(message));
        }
    }

    public static void d(String message){
        if(isDebug){
            // Throwable instance must be created before any methods
            getMethodNames(new Throwable().getStackTrace());
            Log.d(className, createLog(message));
        }
    }

    public static void v(String message){
        if(isDebug){
            // Throwable instance must be created before any methods
            getMethodNames(new Throwable().getStackTrace());
            Log.v(className, createLog(message));
        }
    }

    public static void w(String message){
        if(isDebug){
            // Throwable instance must be created before any methods
            getMethodNames(new Throwable().getStackTrace());
            Log.w(className, createLog(message));
        }
    }





}
